import 'package:enterprise_v6_app/app/components/custom_modele/new_my_columnar_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/train_plan/course_statistics_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/train_plan/people_counting_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/train_plan/training_result_department_list_entity.dart';

///培训结果
class TrainResultController extends GetxController {
  List<MyMultipleXAxisStruct> hiddenDangerAxisList = [];
  double hiddenDangerYAxis = 0;

  /// 人数统计
  PeopleCountingEntity? peopleCountingEntity;

  /// 课程统计
  List<CourseStatisticsEntity>? courseStatisticsEntity;

  /// 列表
  List<TrainingResultDepartmentListEntity>? trainingResultDepartmentListEntity;

  @override
  void onInit() {
    super.onInit();
    //线上培训结果 人数统计
    peopleCounting();
    //线上培训结果 课程统计
    courseStatistics();
  }

  ///课程树状图
  _getCourseThatYearData(List<CourseStatisticsEntity> courseStatisticsEntity) {
    for (int i = 0; i < courseStatisticsEntity.length; i++) {
      hiddenDangerAxisList.add(
        MyMultipleXAxisStruct(
          showsTitle: i == 0 ? true.obs : false.obs,
          names: '${courseStatisticsEntity[i].textbookName}',
          color: [const Color(0xFF5C6DFF), const Color(0xFFFD6B6B)],
          nums: [double.parse('${courseStatisticsEntity[i].qualified}'), double.parse('${courseStatisticsEntity[i].unqualified}')],
        ),
      );
    }
    hiddenDangerYAxis = 40;
    update();
  }

  ///线上培训结果 人数统计
  void peopleCounting() async {
    final res = await HttpUtil().generateGetRequest<PeopleCountingEntity>(
      Interface.peopleCounting,
      queryParameters: {'planId': Get.arguments['id']},
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        peopleCountingEntity = result.data!;
      }
    });
    update();
  }

  ///线上培训结果课程统计
  void courseStatistics() async {
    final res = await HttpUtil().generateGetRequest<List<CourseStatisticsEntity>>(
      Interface.courseStatistics,
      queryParameters: {'planId': Get.arguments['id']},
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        courseStatisticsEntity = result.data!;
        //课程树状图
        _getCourseThatYearData(courseStatisticsEntity ?? []);
        //线上培训结果 部门列表 默认获取第一行的
        trainingResultDepartmentList(textBookId: courseStatisticsEntity?[0].textbookId ?? '');
      }
    });
  }

  ///线上培训结果部门列表
  void trainingResultDepartmentList({String textBookId = ''}) async {
    final res = await HttpUtil().generateGetRequest<List<TrainingResultDepartmentListEntity>>(
      Interface.trainingResultDepartmentList,
      queryParameters: {
        'planId': Get.arguments['id'],
        'textBookId': textBookId,
        'pageNum': 1,
        'pageSize': 1000,
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        trainingResultDepartmentListEntity = result.data!;
        update();
      }
    });
  }
}
